<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Validation\Validator;

use Exception;
use Auth;
use Hash;
use App\Model\ExamCategory;
use App\Model\Exam;
use App\Model\PaperCategory;
use App\Model\Paper;
use App\Model\PaperQuestions;
use App\Model\PaperRecord;
use App\Model\PaperRecordQuestions;
use App\Model\Users;


class ExamController extends Controller
{

    // 前端考试列表 查询所有的试卷列表

    public function paper_list(Request $request){
        $user = Auth::user();
        $query = Paper::with(['category'])->where('is_delete','0')->orderBy('id','desc');
		
		$res=$query->paginate($request->get('limit',10))->toArray();

        foreach($res['data'] as &$v){
            $v['number'] = count(PaperRecord::where(['uid'=>$user['id'],'pid'=>$v['id']])->get()->toArray());
        }
        return response()->json(['data' => $res['data'],'count'=>$res['total'],'message'=>'请求成功','code' => 200], 200);
    }



    // 考试开始页面获取数据

    public function paper_one(Request $request){
        if(!$request['id']){
            return response()->json(['data' => [],'message'=>'试卷id不能为空','code' => 100], 200); 
        }
        
        $res = Paper::with(['category'])->where('is_delete','0')->where('id',$request['id'])->first();
        return response()->json(['data' => $res,'message'=>'请求成功','code' => 200], 200); 
    }




    // 获取考试试题，插入考试记录的数据

    public function paper_answer(Request $request){
        if(!$request['id']){
            return response()->json(['data' => [],'message'=>'试卷id不能为空','code' => 100], 200); 
        }
        $user = Auth::user();
        // dump($user);exit;
        $paper_record = new PaperRecord();
        $paper_record->uid = $user['id'];
        $paper_record->username = $user['name'];
        $paper_record->pid = $request['id'];
        $paper_record->created_at = date('Y-m-d H:i:s',time());
        $paper_record->save();
        // 获取试题数据

        $eid_data = PaperQuestions::where('pid',$request['id'])->select('eid')->get();
		if(!empty($eid_data)){
			$eid_data = $eid_data->toArray();
		}else{
			$eid_data = [];
		}
		
		$eids = array_column($eid_data,'eid');
		
		$res = Exam::whereIn('id',$eids)->get();


        return response()->json(['data' => ['count'=>count($res),'exam'=>$res],'message'=>'请求成功','code' => 200], 200); 
    }


    // 考试结果页面 即 交卷，返回考试答对的题目数量及分数
    public function paper_examination(Request $request){
        if(!$request['id']){
            return response()->json(['data' => [],'message'=>'试卷id不能为空','code' => 100], 200); 
        }
        // 用户信息
        $user = Auth::user();
        
        // 试卷id
        $pid = $request['id'];
        $exam = json_decode($request['exam'],true);
    //    dump($exam);exit;
        // exam数组，包含所有答题的题目id，选择答案 user_answer

        $record = PaperRecord::where(['uid'=>$user['id'],'username'=>$user['name'],'pid'=>$pid])->orderBy('id','desc')->first();
        
        $score = 0;
        $answer_nums = 0;
        foreach($exam as $v){
            $question = new PaperRecordQuestions();
            $exam = Exam::where('id',$v['id'])->first();
            $question->paper_id = $pid;
            $question->user_id = $user['id'];
            $question->exam_id = $v['id'];
            $question->exam_answer = $exam['answer'];
            $question->user_answer = $v['user_answer'];
            $question->record_status = $exam[$exam['answer']]==$exam[$v['user_answer']]?'1':'0';
            $question->record_score = $exam[$exam['answer']]==$exam[$v['user_answer']]?$exam['score']:0;
            $question->record_id = $record['id'];
            $question->save();
            if($question->record_status ==1){
                $score = $score+$exam['score'];
                $answer_nums++;
            }
        }
        $is_pass='0';
        $paper = Paper::where('id',$pid)->first();
        if($score>=$paper['pass_score']){
            $is_pass = '1';
        }
        

        PaperRecord::where(['uid'=>$user['id'],'username'=>$user['name'],'pid'=>$pid])->update(['status'=>4,'submit_at'=>date('Y-m-d H:i:s',time()),'duration'=>$request['duration'],'score'=>$score,'is_pass'=>$is_pass]);
        return response()->json(['data' => ['answer_nums'=>$answer_nums,'score'=>$score,'record_id'=>$record['id']],'message'=>'请求成功','code' => 200], 200); 

    }

    // 错题汇总列表
    public function answer_wrong(Request $request){
        if(!$request['id']){
            return response()->json(['data' => [],'message'=>'试卷id不能为空','code' => 100], 200); 
        }
        $pid = $request['id'];
        $record_id = $request['record_id'];
        $user = Auth::user();
        $res = PaperRecordQuestions::where(['paper_id'=>$pid,'record_id'=>$record_id,'user_id'=>$user['id'],'record_status'=>'0'])->get();
        // dump($user);exit;
        foreach($res as &$v){
            $exam = Exam::where('id',$v['exam_id'])->first();
            $v['exam'] = $exam;
        }
        return response()->json(['data' => ['count'=>count($res),'wrong_exam'=>$res],'message'=>'请求成功','code' => 200], 200); 
    }

    // 考试记录
    public function user_exam_record(Request $request){
        $user = Auth::user();
        $data = PaperRecord::with(['paper:id,title'])->where('uid',$user['id'])->orderBy('created_at','desc')->paginate($request->get('limit',10))->toArray();
        return response()->json(['count'=>$data['total'],'data'=>$data['data'],'message'=>'请求成功','code' => 200], 200); 
        // return response()->json(['data' => ['count'=>$data['total'],'exam'=>$data['data']],'message'=>'请求成功','code' => 200], 200); 

    }

    // 考试记录的详情
    public function record_detail(Request $request){
        $user = Auth::user();
        if(!$request['id']){
            return response()->json(['data' => [],'message'=>'考试记录id不能为空','code' => 100], 200); 
        }
        $res = PaperRecordQuestions::where(['record_id'=>$request['id'],'user_id'=>$user['id']])->get();
        // dump($user);exit;
        foreach($res as &$v){
            $exam = Exam::where('id',$v['exam_id'])->first();
            $v['exam'] = $exam;
        }
        // return response()->json(['count'=>count($res),'exam'=>$res,'message'=>'请求成功','code' => 200], 200); 
        return response()->json(['data' => ['count'=>count($res),'exam'=>$res],'message'=>'请求成功','code' => 200], 200); 
    }
}
